<?php
class AdminsController extends AppController {
    public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Session', 'Image');

    public function view($id) {
        $this->Post->id = $id;
        $this->set('post', $this->Post->read());
    }

    public function add() {
        if ( $this->request->is('post') ) {
            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash('Your post has been saved.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Unable to add your post.');
            }
        }
    }
    
    public function index() {
        $this->layout = 'admin';
        $this->set( 'pages',    $this->Admin->getPages() );
        $this->set( 'diensten', $this->Admin->getDiensten() );
        $this->set( 'newsitems', $this->Admin->getNewsItems() );
    }
    
    public function editPage( $page = null ) {
        $this->layout = 'admin';
        if ($this->request->is('post')) {
            $this->Admin->set($this->data);
            $this->set('content', array( 'titel'   => $this->data['Admin']['titel'], 
                                         'content' => $this->data['Admin']['content'],
                                         'page'    => $this->data['Admin']['page']) );
            if ( $this->Admin->validates() ) {
                $this->Admin->savePage($this->data);
                $this->redirect(array('action' => 'index'));
            }
        }
        else if ($page != null) {
            $this->set('content', $this->Admin->getPage($page));
        }
        
    }

    public function editDienst( $dienst = null ) {
        $this->layout = 'admin';

        if ($this->request->is('post')) {
            $this->Admin->set($this->data);
            $this->set('content', array( 'titel'   => $this->data['Admin']['titel'],
                                         'content' => $this->data['Admin']['content'],
                                         'type'    => $this->data['Admin']['type']) );
            if ( $this->Admin->validates() ) {
                $this->Admin->saveDienst($this->data);
                $this->redirect(array('action' => 'index'));
            }
        }
        else if ($dienst != null) {
            $this->set('content', $this->Admin->getDienst($dienst));
        }
    }
    
    public function submit() {
        $this->layout = 'admin';
        
        if ($this->request->is('post')) {
            $this->Admin->set($this->data);
            if ( $this->Admin->validates() ) {
                $this->Admin->savePage($this->data);
//                 $this->redirect(array('action' => 'index'));
            }
        }
    }

    public function submit_dienst() {
        $this->layout = 'admin';
    
        if ($this->request->is('post')) {
            $this->Admin->set($this->data);
            if ( $this->Admin->validates() ) {
                $this->Admin->saveDienst($this->data);
                $this->redirect(array('action' => 'index'));
            }
        }
    }

    public function addNewsItem() {
        $this->layout = 'admin';
        
        if ($this->request->is('post')) {
            $this->Admin->set($this->data);

            if ( $this->Admin->validates() ) {
                $key = $this->Admin->addNewsItem($this->data);
                
                $imgArray = array();
                $imgArray['type'] = 'newsItem';
                $imgArray['key']  = $key;
                $imgArray['images'] = array();
                $imgArray['thumbs'] = array();
                
                for ($i = 1; $i < 11; $i++) {
                    $photo = 'foto'.$i;
                    if (array_key_exists($photo, $this->data['Admin']) && $this->data['Admin'][$photo]['name'] != '') {
                        $target_path = "img/upload/";
                        $target_path = $target_path.$this->data['Admin'][$photo];
                        $this->Image->set_paths(WWW_ROOT . 'img/upload/', WWW_ROOT . 'img/thumb/');
                        $file = $this->Image->upload_image('Admin.'.$photo);
                        $imgArray['images'][$i] = "$file";
                        $file = $this->Image->thumb($file);
                        $imgArray['thumbs'][$i] = "$file";
                    }
                }
                
                if ( !empty($imgArray['images']) ) {
                    $this->Admin->addImages($imgArray);
                }
               $this->redirect(array('action' => 'index'));
            }
        }
    }
    
    public function editNewsItem( $page = null ) {
        $this->layout = 'admin';
        if ($this->request->is('post')) {
            $this->Admin->set($this->data);
            $this->set('content', array( 'titel'   => $this->data['Admin']['titel'], 
                                         'content' => $this->data['Admin']['content'],
                                         'datum'   => $this->data['Admin']['datum'],
                                         'image'   => $this->data['Admin']['image'],
                                         'key'     => $this->data['Admin']['key']) );
            if ( $this->Admin->validates() ) {
                $this->Admin->saveNewsItem($this->data);
                $this->redirect(array('action' => 'index'));
            }
        }
        else if ($page != null) {
            $this->set('content', $this->Admin->getNewsItem( $page ));
        }
        
        $this->set('images', $this->Admin->getImages('newsItem', $page));
    }
        
    public function deleteNewsItem( $item = null ) {
        $this->layout = 'admin';
        
        if ( $item != null ) {
            $this->Admin->deleteNewsItem( $item );
            $this->redirect(array('action' => 'index'));
        }
    }
}